<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 报备管理相关功能 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use User\Api\UserApi;

/**
 * 报备管理
 * @author 麦当苗儿 <changziming@vchaoxi.com>
 */
class BaobeiController extends AdminController {

    /**
     * 用户报备记录 
     */
    public function index(){ 
        $keywords           = I('keywords');  
        if(is_numeric($keywords)){ 
            $map['tel']      =   array('like','%'.$keywords.'%') ;
        }else{
            $map['name']    =   array('like', '%'.(string)$keywords.'%');
        }
        $projectId = I('project_id');
        if( $projectId ){
            $map['project_id'] = $projectId ; 
        }
        if( isset($_GET['status']) ){
            $status = I('status');
            if($status!='all'){
                $map['status'] = $status ; 
            }
            $this->assign('vst',$status);
        }
        $list   = $this->lists('Baobei', $map);   
        $this->assign('_list', $list);
        $this->meta_title = '报备记录';
        $projectList = M('project')->order('sort ASC')->select();
        $this->assign('projectList', $projectList); 
        $this->display();
    } 


    /**
     * 报备状态修改
     * @author Dreamy <changziming@vchaoxi.com>
     */
    public function changeStatus($status=0){
        $id = array_unique((array)I('id',0)); 
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);  
        M('baobei')->where($map)->save(array('status'=>$status));
        $this->success("操作成功"); 
    } 
    
    /**
    * 删除报备记录
    * post方式的循环删除和get方式的单个删除 
    * @author changziming <294207330@qq.com>
    */
    public function del(){
        if(IS_POST){
            $ids = I('post.ids');
            $baobei = M("baobei"); 
            if(is_array($ids)){
                foreach($ids as $id){ 
                    $baobei->where("id='$id'")->delete(); 
                }
            }
            $this->success("删除成功！");
        }else{
            $id = I('get.ids');
            $db = M("baobei");
            $status = $db->where("id='$id'")->delete(); 
            if ($status){
                $this->success("删除成功！");
            }else{
                $this->error("删除失败！");
            }
        } 
    }

    /**
     * 项目列表 
     */
    public function project(){ 
        $title           = I('title'); 
        $map['status']   =   array('egt',0); # 0为禁用 -1 为删除  
        $map['title']    =   array('like', '%'.(string)$title.'%'); 
        $list   = $this->lists('Project', $map);  
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '项目管理';
        $this->display();
    } 

     /**
     * 新增项目
     * @author changziming.com <294207330@qq.com>
     */
    public function projectadd(){
        if(IS_POST){
            if ( I('title') == '') {
                $this->error("项目名称不能为空");
                exit() ; 
            }
            $_POST['create_time']   = time() ; 
            $project                  = M('project');
            $data                   = $project->create(); 
            if($data){
                if($project->add()){  
                    $this->success('新增成功', U('project'));
                } else {
                    $this->error('新增失败');
                } 
            } else {
                $this->error($zu->getError()) ;
            }
        } else {
            $this->meta_title = '新增项目';  
            $this->assign('info',null);  
            $this->display('projectedit');
        }
    }

    /**
     * 编辑项目资料 
     * @author changziming <294207330@qq.com>
     */
    public function projectedit($id = 0){
        if(IS_POST){ 
            if($_POST["id"]){ 
                $data = $_POST ;   
                $result = M('project')->where("id='$id'")->save($data);
                if($result!==false){  
                    $this->success('更新成功',U('project') ) ;
                } else {
                    $this->error('更新失败55'.$id);
                }
            } else {
                $this->error(M()->getError());
            }
        } else {
            $info = array();
            $info = M('project')->find($id);  
            $this->assign('info',$info); 
            $this->meta_title = '编辑项目';
            $this->display('projectedit');
        }
    }

     /**
     * 项目状态修改
     * @author Dreamy <changziming@vchaoxi.com>
     */
    public function projectchangeStatus($status=0){
        $id = array_unique((array)I('id',0)); 
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);  
        M('project')->where($map)->save(array('status'=>$status));
        $this->success("操作成功"); 
    }

    /**
    * 删除项目记录
    * post方式的循环删除和get方式的单个删除 
    * @author changziming <294207330@qq.com>
    */
    public function projectdel(){
        if(IS_POST){
            $ids = I('post.ids');
            $Project = M("Project"); 
            if(is_array($ids)){
                foreach($ids as $id){ 
                    $Project->where("id='$id'")->delete(); 
                }
            }
            $this->success("删除成功！");
        }else{
            $id = I('get.ids');
            $db = M("Project");
            $status = $db->where("id='$id'")->delete(); 
            if ($status){
                $this->success("删除成功！");
            }else{
                $this->error("删除失败！");
            }
        } 
    }
    
    /**
     * 项目动态列表 
     */
    public function article(){ 
        $title           = I('title'); 
        $map['status']   =   array('egt',0); # 0为禁用 -1 为删除  
        $map['title']    =   array('like', '%'.(string)$title.'%'); 
        $list   = $this->lists('article', $map,'sort ASC,create_time DESC');  
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '项目动态';
        $this->display();
    } 

    /**
     * 项目动态状态修改
     * @author Dreamy <changziming@vchaoxi.com>
     */
    public function articlechangeStatus($status=0){
        $id = array_unique((array)I('id',0)); 
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);  
        M('article')->where($map)->save(array('status'=>$status));
        $this->success("操作成功"); 
    }

     /**
     * 新增项目动态
     * @author changziming.com <294207330@qq.com>
     */
    public function articleadd(){
        if(IS_POST){
            if ( I('title') == '') {
                $this->error("动态名称不能为空");
                exit() ; 
            }
            $_POST['create_time']   = time() ; 
            $article                = M('article');
            $data                   = $article->create(); 
            if($data){
                if($article->add()){  
                    $this->success('新增成功', U('article'));
                } else {
                    $this->error('新增失败');
                } 
            } else {
                $this->error($article->getError()) ;
            }
        } else {
            $this->meta_title = '新增项目动态';  
            $this->assign('info',null);  
            $this->display('articleedit');
        }
    }

    /**
     * 编辑项目动态 
     * @author changziming <294207330@qq.com>
     */
    public function articleedit($id = 0){
        if(IS_POST){ 
            if($_POST["id"]){ 
                $data = $_POST ;   
                $result = M('article')->where("id='$id'")->save($data);
                if($result!==false){  
                    $this->success('更新成功',U('article') ) ;
                } else {
                    $this->error('更新失败55'.$id);
                }
            } else {
                $this->error(M()->getError());
            }
        } else {
            $info = array();
            $info = M('article')->find($id);  
            $this->assign('info',$info); 
            $this->meta_title = '编辑项目动态';
            $this->display('articleedit');
        }
    }

      /**
    * 删除动态记录
    * post方式的循环删除和get方式的单个删除 
    * @author changziming <294207330@qq.com>
    */
    public function articledel(){
        if(IS_POST){
            $ids = I('post.ids');
            $Article = M("Article"); 
            if(is_array($ids)){
                foreach($ids as $id){ 
                    $Article->where("id='$id'")->delete(); 
                }
            }
            $this->success("删除成功！");
        }else{
            $id = I('get.ids');
            $db = M("Article");
            $status = $db->where("id='$id'")->delete(); 
            if ($status){
                $this->success("删除成功！");
            }else{
                $this->error("删除失败！");
            }
        } 
    }
    
}